package com.lovo.frame;

import com.lovo.pojo.UserDO;
import com.lovo.service.UserService;
import com.lovo.service.impl.UserServiceImpl;
import com.lovo.util.Util;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GetPanel extends JPanel implements ActionListener {
    //    private Icon bgOff = Util.getIcon("/images/buttonOff.jpg");
//    private Icon bgsure = Util.getIcon("/images/buttonSure.jpg");
    private ATM frame;
    private JTextField textField = new JTextField();
    private JButton offButton = new JButton("退出");
    private JButton sureButton = new JButton("确认");
private UserService service=new UserServiceImpl();
    public GetPanel(ATM frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);
        sureButton.setBounds(300, 500, 100, 50);
        this.add(sureButton);
        sureButton.addActionListener(this);
        JLabel label = new JLabel("取款金额");
        label.setBounds(350, 300, 100, 20);
        this.add(label);
        textField.setBounds(400, 300, 100, 20);
        this.add(textField);
        offButton.setBounds(300, 600, 100, 50);
        this.add(offButton);
        offButton.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == offButton) {
            frame.switchPanel("menu");
        } else if (e.getSource() == sureButton) {
            double shuru = Double.valueOf(textField.getText());
//            if (shuru > yue) {
//                JOptionPane.showMessageDialog(this, "余额不足");
//            } else {
//                double qu = yue - shuru;
//                frame.getLoginUser().setMoney(qu);
//                Util db = new Util();
//                UserDO loginUser = db.getById(frame.getLoginUser().getId());
//                loginUser.setMoney(loginUser.getMoney() - shuru);
//                db.update(loginUser);
//                frame.switchPanel("menu");
//            }
            try {
                service.getMoney(frame.getLoginUser().getId(),shuru);
                frame.switchPanel("menu");
            } catch (Exception exception) {
                JOptionPane.showMessageDialog(this, "余额不足");
            }
        }
    }
}
